package cn.edu.pku.controller;

import cn.edu.pku.constant.EnumStatus;
import cn.edu.pku.entity.BookNote;
import cn.edu.pku.entity.BookNoteDetail;
import cn.edu.pku.service.BookNoteService;
import cn.edu.pku.vo.BookNoteResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by ming.qin on 2017/4/21.
 */
@RestController
@Slf4j
public class BookNoteController {
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private BookNoteService bookNoteService;

    /**
     * 列出所有读书笔记
     *
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/listnote", method = {RequestMethod.GET})
    public BookNoteResult listBooknote(@RequestParam("page") int page, @RequestParam("size") int size,@RequestParam(value = "bookName",required = false) String bookName,@RequestParam(value = "author",required = false) String author) {
//        page = page -1;
        BookNoteResult result = new BookNoteResult();
        if(page < 0 ){
            result.setStatus(EnumStatus.FAILED.getStatus());
            result.setMsg("页数必须大于等于1:"+page);
        }else {
//            bookNoteService.listBookNote(page, size, bookName, author)
//            result.setData(list);
            result.setStatus(EnumStatus.SUCCESS.getStatus());
            PageInfo<BookNote> list = bookNoteService.listBookNote(page,size,bookName,author);
            result.setData(list);
        }
        return result;
    }

    /**
     * 展示单个读书笔记
     *
     * @param id noteId
     * @param bookId bookId
     * @return
     */
    @RequestMapping(value = "/note", method = {RequestMethod.GET})
    public BookNoteResult getBooknote(@RequestParam(value = "id", required = false) Integer id, @RequestParam(value = "bookId", required = false) Integer bookId) {
        BookNote note = new BookNote();
        if (id == null) {
            note = bookNoteService.getBookNoteByBookId(bookId);
        } else {
            note = bookNoteService.getBookNote(id);
        }
        if (note == null) {
            note = new BookNote();
        }
        BookNoteResult result = new BookNoteResult();
        result.setStatus(EnumStatus.SUCCESS.getStatus());
        result.setData(note);
        return result;
    }

    /**
     * 提供给首页，top3读书笔记
     *
     * @return
     */
    @RequestMapping(value = "/listnoteTop",method = RequestMethod.GET)
    @ApiOperation(value = "首页列出读书笔记", notes = "")
    @ApiResponse(response = BookNoteResult.class, message = "", code = 200)
    public BookNoteResult listBooknoteTop() {
        BookNoteResult result = new BookNoteResult();
        result.setStatus(EnumStatus.SUCCESS.getStatus());
        result.setData(bookNoteService.listBookNoteTop(3));
        return result;
    }

    /**
     * 插入笔记
     * @param bookId 图书id
     * @param title title
     * @param content 内容
     * @param beginTime  开始时间
     * @param endTime  结束时间
     */
    @RequestMapping(value = "/insertNote",method = {RequestMethod.POST})
    public BookNoteResult insertNote(@RequestParam("bookId")Integer bookId, @RequestParam("title") String title, @RequestParam("content") String content, @RequestParam(value = "beginTime",required = false) String beginTime, @RequestParam(value = "endTime",required = false) String endTime){
        Date beginTimeDate = null ;
        Date endTimeDate  = null;

        try {
            if (!StringUtils.isEmpty(beginTime)) {
                beginTimeDate = format.parse(beginTime);
            }
            if(!StringUtils.isEmpty(endTime)) {
                endTimeDate = format.parse(endTime);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return getErrorResult("日期格式错误");
        }
        BookNote bookNote = new BookNote();
        bookNote.setBookId(bookId);
        bookNote.setTitle(title);
        bookNote.setContent(content);
        bookNote.setBeginTime(beginTimeDate);
        bookNote.setEndTime(endTimeDate);
        Date now = new Date();
        bookNote.setcTime(now);
        bookNote.setuTime(now);
        int code = bookNoteService.insertBookNote(bookNote);
        return getResult(code,"插入成功","插入失败");
    }



    @RequestMapping(value = "/updateNote",method={RequestMethod.POST})
    public BookNoteResult updateNote(@RequestParam("id")Integer id,@RequestParam("bookId")Integer bookId, @RequestParam("title") String title, @RequestParam("content") String content, @RequestParam(value = "beginTime",required = false) String beginTime, @RequestParam(value = "endTime",required = false) String endTime){
        Date beginTimeDate = null;
        Date endTimeDate = null;
        try {
            if (!StringUtils.isEmpty(beginTime)) {
                beginTimeDate = format.parse(beginTime);
            }
            if(!StringUtils.isEmpty(endTime)) {
                endTimeDate = format.parse(endTime);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return getErrorResult("日期格式错误");
        }
        BookNote bookNote = new BookNote();
        bookNote.setId(id);
        bookNote.setBookId(bookId);
        bookNote.setTitle(title);
        bookNote.setContent(content);
        bookNote.setBeginTime(beginTimeDate);
        bookNote.setEndTime(endTimeDate);
        Date now = new Date();
        bookNote.setuTime(now);
        int code = bookNoteService.updateBookNote(bookNote);
        return getResult(code,"更新成功","更新失败");
    }

    /**
     * 删除笔记
     * @param noteId
     */
    @RequestMapping(value="/deleteNote",method = {RequestMethod.POST})
    public BookNoteResult deleteNote(@RequestParam("bookId")Integer noteId){
        int code = bookNoteService.deleteNote(noteId);
        return getResult(code,"删除成功","删除失败");
    }

    /**
     * 查询笔记
     * @param bookName 书名
     * @param author 作者
     * @return 列表
     */
    @RequestMapping(value="/queryNote",method = {RequestMethod.GET})
    public BookNoteResult queryNote(@RequestParam("bookName" ) String bookName,@RequestParam("author") String author){
        if(StringUtils.isEmpty(bookName)){
            return getErrorResult("请输入书名");
        }
        if(StringUtils.isEmpty(author)){
            return getErrorResult("请输入作者");
        }
        List<BookNote> bookNoteList =  bookNoteService.queryNote(bookName,author);
        BookNoteResult result = new BookNoteResult();
        result.setStatus(EnumStatus.SUCCESS.getStatus());
        result.setData(bookNoteList);
        return result;
    }

    /**
     * 查询笔记明细
     * @param id 笔记id
     * @return 明细
     */
    @RequestMapping(value="/getNoteDetail",method = {RequestMethod.GET})
    public BookNoteResult getNoteDetail(@RequestParam("id")Integer id){
        BookNoteDetail detail = bookNoteService.getNoteDetail(id);
        BookNoteResult result = new BookNoteResult();
        result.setStatus(EnumStatus.SUCCESS.getStatus());
        result.setData(detail);
        return result;
    }

    /**
     * 获取反馈结果
     * @param code code
     * @param successMsg 成功反馈消息
     * @param failedMsg 失败反馈消息
     * @return 结果
     */
    private BookNoteResult getResult(int code,String successMsg,String failedMsg){
        BookNoteResult result = new BookNoteResult();
        if(code >= 1){
            result.setStatus(EnumStatus.SUCCESS.getStatus());
            result.setMsg(successMsg);
        }else{
            result.setStatus(EnumStatus.FAILED.getStatus());
            result.setMsg(failedMsg);
        }
        return result;
    }

    /**
     * 生成错误信息
     * @param errorMsg 错误信息
     * @return 错误信息
     */
    private BookNoteResult getErrorResult(String errorMsg){
        BookNoteResult result = new BookNoteResult();
        result.setStatus(EnumStatus.FAILED.getStatus());
        result.setMsg(errorMsg);
        return result;
    }
}
